/**
 * 
 */
package ads.engenharia.bl;

import java.sql.SQLException;
import java.util.ArrayList;

import ads.engenharia.dal.DisciplineDAO;
import ads.engenharia.dal.DisciplineProfessorDAO;
import ads.engenharia.dal.DisciplineScheduleDAO;
import ads.engenharia.entity.Course;
import ads.engenharia.entity.Discipline;
import ads.engenharia.entity.Professor;
import ads.engenharia.entity.Schedule;

/**
 * @author Lanedraex
 *
 */
public class DisciplineBO {

	private DisciplineDAO _disciplineDAO;
	private DisciplineScheduleDAO _disciplineScheduleDAO;
	private DisciplineProfessorDAO _disciplineProfessorDAO;

	public DisciplineBO() {

		_disciplineDAO = new DisciplineDAO();
		_disciplineScheduleDAO = new DisciplineScheduleDAO();
		_disciplineProfessorDAO = new DisciplineProfessorDAO();
	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void save(Discipline discipline) throws SQLException {

		discipline.id(_disciplineDAO.save(discipline).id());
		_disciplineScheduleDAO.save(discipline);

	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void associateDisciplineProfessor(Discipline discipline)
			throws SQLException {

		_disciplineProfessorDAO.save(discipline);
	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void delete(Discipline discipline) throws SQLException {

		_disciplineDAO.delete(discipline);

	}

	/**
	 * 
	 * @param discipline
	 * @throws SQLException
	 */
	public void update(Discipline discipline) throws SQLException {

		_disciplineDAO.update(discipline);
		_disciplineScheduleDAO.update(discipline);
		_disciplineProfessorDAO.update(discipline);

	}

	/**
	 * 
	 * @param discipline
	 * @return
	 * @throws SQLException
	 */
	public Discipline search(Discipline discipline) throws SQLException {

		discipline = _disciplineDAO.search(discipline);

		// Procura pelo schedule para completar a disciplina
		discipline.schedule(searchDisciplineSchedule(discipline));

		// Procura pelo professor para completar a disciplina
		discipline.professor(searchDisciplineProfessor(discipline));

		return discipline;
	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchAll() throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchAll();

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;

	}

	/**
	 * 
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public Discipline searchById(int id) throws SQLException {

		Discipline discipline;
		discipline = _disciplineDAO.searchById(id);

		// Procura pelo schedule para completar a disciplina
		discipline.schedule(searchDisciplineSchedule(discipline));

		// Procura pelo professor para completar a disciplina
		discipline.professor(searchDisciplineProfessor(discipline));

		return discipline;

	}

	/**
	 * 
	 * @return
	 * @throws SQLException
	 */
	public Discipline searchByMaxId() throws SQLException {

		Discipline discipline;
		discipline = _disciplineDAO.searchByMaxId();

		// Procura pelo schedule para completar a disciplina
		discipline.schedule(searchDisciplineSchedule(discipline));

		// Procura pelo professor para completar a disciplina
		discipline.professor(searchDisciplineProfessor(discipline));

		return discipline;

	}

	/**
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByName(String name) throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchByName(name);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;

	}

	/**
	 * 
	 * @param duration
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByDuration(int duration)
			throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchByDuration(duration);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;

	}

	/**
	 * 
	 * @param professor
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByProfessor(Professor professor)
			throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchByProfessor(professor);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;
	}

	/**
	 * 
	 * @param day
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByDay(Schedule.Day day)
			throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchByDay(day);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;
	}

	/**
	 * 
	 * @param time
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByTime(Schedule.Time time)
			throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchByTime(time);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;
	}

	/**
	 * 
	 * @param hour
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Discipline> searchByHour(Schedule.Hour hour)
			throws SQLException {

		ArrayList<Discipline> disciplines;
		disciplines = _disciplineDAO.searchByHour(hour);

		for (Discipline d : disciplines) {
			// Procura pelo schedule para completar a disciplina
			d.schedule(searchDisciplineSchedule(d));

			// Procura pelo professor para completar a disciplina
			d.professor(searchDisciplineProfessor(d));

		}

		return disciplines;
	}

	public ArrayList<Discipline> searchByCourse(String courseName)
			throws SQLException {

		CourseBO courseBO = new CourseBO();
		ArrayList<Course> courses = courseBO.searchByName(courseName);
		ArrayList<Discipline> disciplines = new ArrayList<Discipline>();

		for (Course c : courses)
			disciplines.addAll(c.program());

		return disciplines;

	}

	/**
	 * 
	 * @param discipline
	 * @return
	 * @throws SQLException
	 */
	private Schedule searchDisciplineSchedule(Discipline discipline)
			throws SQLException {

		return _disciplineScheduleDAO.searchDisciplineSchedule(discipline);
	}

	/**
	 * 
	 * @param discipline
	 * @return
	 * @throws SQLException
	 */
	private Professor searchDisciplineProfessor(Discipline discipline)
			throws SQLException {

		return _disciplineProfessorDAO.searchDisciplineProfessor(discipline);
	}
}
